package com.zmc.lostfound.post.controller;

import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.post.entity.CommentEntity;
import com.zmc.lostfound.post.service.CommentService;
import com.zmc.lostfound.post.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZMC
 * @description 评论管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:49:44
 */
@RestController
@RequestMapping("/comment")
@Api(value = "评论管理接口", tags = "评论管理接口")
public class CommentController {

    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @ApiOperation("用户发布评论")
    @PostMapping("/publish")
    public Result publish(@RequestBody @Validated CommentPublishVO commentPublishVO) {
        return commentService.publish(commentPublishVO);
    }

    @ApiOperation("按主键id删除评论")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        return commentService.deleteById(id);
    }

    @ApiOperation("按帖子id批量删除评论")
    @DeleteMapping("/deleteBatchByPostId/{postId}")
    public Result deleteBatchByPostId(@PathVariable("postId") Long postId) {
        return commentService.deleteBatchByPostId(postId);
    }

    @ApiOperation("按主键id查询评论基本信息")
    @GetMapping("/query/infoById/{id}")
    public Result queryInfoById(@PathVariable("id") Long id) {
        CommentEntity commentInfo = commentService.queryInfoById(id);
        return Result.ok("查询成功").put("commentInfo", commentInfo);
    }

    @ApiOperation("按帖子id查询所有评论")
    @GetMapping("/query/infoByPostId/{postId}/{isLikeNumberSort}")
    public Result queryInfoByPostId(@PathVariable("postId") Long postId, @PathVariable("isLikeNumberSort") Boolean isLikeNumberSort) {
        List<CommentDetailInfoVO> commentDetailInfoVOList = commentService.queryInfoByPostId(postId, isLikeNumberSort);
        return Result.ok("查询成功").put("commentDetailInfoVOList", commentDetailInfoVOList);
    }

    @ApiOperation("按帖子id查询评论总数")
    @GetMapping("/query/countByPostId/{postId}")
    public Result queryCountByPostId(@PathVariable("postId") Long postId) {
        if (postId <= 0) {
            return Result.error("查询失败: 帖子id必须大于0");
        }
        Integer count = commentService.query().eq("post_id", postId).count();
        return Result.ok("查询成功").put("count", count);
    }

    @ApiOperation("按帖子id查询所有一级评论")
    @GetMapping("/query/oneLevelByPostId/{postId}/{isLikeNumberSort}")
    public Result queryOneLevelByPostId(@PathVariable("postId") Long postId, @PathVariable("isLikeNumberSort") Boolean isLikeNumberSort) {
        List<CommentDetailInfoVO> oneLevelCommentList = commentService.queryOneLevelByPostId(postId, isLikeNumberSort);
        return Result.ok("查询成功").put("oneLevelCommentList", oneLevelCommentList);
    }

    @ApiOperation("按父评论id查询所有子评论")
    @GetMapping("/query/childByParentId/{parentId}/{isLikeNumberSort}")
    public Result queryChildByParentId(@PathVariable("parentId") Long parentId, @PathVariable("isLikeNumberSort") Boolean isLikeNumberSort) {
        List<CommentDetailInfoVO> commentDetailInfoVOList = commentService.queryChildByParentId(parentId, isLikeNumberSort);
        return Result.ok("查询成功").put("commentDetailInfoVOList", commentDetailInfoVOList);
    }

    @ApiOperation("按父评论id查询所有扁平化子评论")
    @GetMapping("/query/flatChildByParentId/{parentId}/{isLikeNumberSort}")
    public Result queryFlatChildByParentId(@PathVariable("parentId") Long parentId, @PathVariable("isLikeNumberSort") Boolean isLikeNumberSort) {
        List<FlatChildCommentVO> flatChildCommentVOList = commentService.queryFlatChildByParentId(parentId, isLikeNumberSort);
        return Result.ok("查询成功").put("flatChildCommentVOList", flatChildCommentVOList);
    }

    @ApiOperation("按用户id查询评论的回复消息")
    @GetMapping("/query/replyByUserId/{userId}")
    public Result queryReplyByUserId(@PathVariable("userId") Long userId) {
        List<ReplyCommentVO> replyCommentVOList = commentService.queryReplyByUserId(userId);
        return Result.ok("查询成功").put("replyCommentVOList", replyCommentVOList);
    }

    @ApiOperation("用户点赞评论")
    @PutMapping("/like")
    public Result like(@RequestBody @Validated CommentLikeVO commentLikeVO) {
        return commentService.like(commentLikeVO);
    }

}
